package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.HrRoleAddDTO;
import org.jsola.hr.dto.HrRoleUpdateDTO;
import org.jsola.hr.provider.IPermissionProviderService;
import org.jsola.hr.query.HrRoleQuery;
import org.jsola.hr.service.IHrRoleService;
import org.jsola.hr.vo.HrRoleListVO;
import org.jsola.hr.vo.HrRoleVO;
import org.jsola.permission.exception.PermissionException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;

import static org.jsola.admin.constant.AdminPermissionConstants.PERMISSION_DENIED_MESSAGE;
import static org.jsola.admin.constant.AdminPermissionConstants.SUPPER_ROLE_ROLE;


/**
 * 人力角色表
 *
 * @author zhr
 */
@RestController("hrHrRoleController")
@RequestMapping(HrConstants.API_PATH + "/v1/hrRoles")
public class HrRoleController implements IHrRoleControllerAPI {

    @Autowired
    private IHrRoleService hrRoleService;

    @Autowired
    private IPermissionProviderService permissionProviderService;

    @Override
    @PostMapping
    public Result<HrRoleVO> save(@Valid @RequestBody HrRoleAddDTO hrRoleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增人力角色表权限校验

        return Result.success(
                hrRoleService.save(hrRoleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody HrRoleUpdateDTO hrRoleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改人力角色表权限校验

        int count = hrRoleService.update(hrRoleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{hrRoleId}")
    public Result<Integer> delete(@PathVariable Long hrRoleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除人力角色表权限校验

        int count = hrRoleService.deleteByIds(tokenUser, hrRoleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] hrRoleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除人力角色表权限校验

        int count = hrRoleService.deleteByIds(tokenUser, hrRoleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{hrRoleId}")
    public Result<HrRoleVO> findById(@PathVariable Long hrRoleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力角色表权限校验

        return Result.success(
                hrRoleService.selectById(hrRoleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<HrRoleListVO>> listAll(@Valid HrRoleQuery hrRoleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力角色表权限校验

        return Result.success(
                hrRoleService.selectAndInitialization(hrRoleQuery, tokenUser)
        );
    }


    @Override
    @GetMapping(value = "/selectChannelHrRole")
    public Result<HrRoleVO> selectChannelHrRole() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力角色表权限校验

        return Result.success(
                hrRoleService.selectChannelHrRole(tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listPage")
    public Result<Page<HrRoleListVO>> listPage(@Valid HrRoleQuery hrRoleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力角色表权限校验

        return Result.success(
                hrRoleService.selectAndInitializationPage(hrRoleQuery, tokenUser)
        );
    }

    @Override
    @GetMapping
    public Result<Page<HrRoleListVO>> page(@Valid HrRoleQuery hrRoleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                hrRoleService.selectPage(hrRoleQuery, tokenUser.getSiteId())
        );
    }

    @PostMapping("/deleteRoles")
    @Override
    public Result<String> deleteRoles(@RequestBody @Validated Long[] roleIds) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notEmpty(roleIds, "请选择角色");

        PermissionException.isTrue(permissionProviderService.hasRight(tokenUser.getUserId()
                , tokenUser.getSiteId(), SUPPER_ROLE_ROLE), PERMISSION_DENIED_MESSAGE);
        Integer integer = hrRoleService.deleteRoles(Arrays.asList(roleIds), tokenUser);
        ParamException.isTrue(integer > 0, "删除失败，请刷新后重试");
        return Result.success();
    }

}

